import processing.video.*;
PrintWriter output;
Capture cam;
Float pulse[] = new Float[290];
int i = 292;
int view = 0;
int write = 1;
String filename;
graph grp;
File file;
//dialogbox dgbx;
void setup() {
  size(800, 600);
  grp = new graph();
  // If no device is specified, will just use the default.
  cam = new Capture(this, 320, 240);
  
//  dgbx = new dialogbox("Camera not found"); 
  //String[] devices = Capture.list();
  //println(devices);
  // Change devices[0] to the proper index for your camera.
  //cam = new Capture(this, width, height, devices[0]);
  cam.settings();
  PFont font = loadFont("Helvetica-12.vlw");
  frameRate(8);
}


void draw() {
  background(209);
// println(i); //For debug purposes
  if(view == 0){
  image(loadImage("btnrc.png"),425,150);
  if(i<290){
    
    if (cam.available() == true) {
      image(loadImage("rec.png"),(width/2)-140,(height/2)-28);
      cam.loadPixels();
      cam.read();
      image(cam,0,0);
      if(i>29){
      float sum = 0;
      int index = 0;
      for (int y = 0; y < cam.height; y++) {
        for (int x = 0; x < cam.width; x++) {
          // Get the color stored in the pixel
          color pixelValue = cam.get(x,y);
          sum = red(pixelValue) + sum;
          index++;
          //
        }
      }
      float bob = sum/(index+1);
      pulse[i-30] = bob;
      
      }
      i++;
      
    }
    else{
    
    }
  }
  else {
    if(i==290){
      cam.loadPixels();
      cam.read();
      image(cam,0,0);
      grp.paint(pulse,240,0,0); //240 orignal
    }
    else{
      cam.loadPixels();
      cam.read();
      image(cam,0,0);
    }
  }
  }
  else{
    if(view == 1){
      filename = selectOutput("Create a file to export to.");
      output =  createWriter(filename);
      view = 0; 
      write = 0;
    }
    else{
      if(view == 2){
        
      }
      else{
        
      }
    }
  }
}


void mouseClicked(){
  if(mouseX >= 425 && mouseX <= 673 && mouseY >=150 && mouseY <=192){
    i = 0;
  }
}
